<template>
	<el-card>
		<el-form :inline="true" ref="dataFormRef" :model="dataForm" :rules="loginRules" @keyup.enter="getDataList()">
			<div class="divline">
				<el-form-item class="divlineleft" label="入库方式:">
					<el-select v-model="dataForm.addType">
						<el-option
						v-for="item in dataForm.typelist"
						:key="item.value"
						:label="item.label"
						:value="item.value"
						/>
					</el-select>
				</el-form-item>
				<el-form-item class="divlinecenter" prop="addTime" label="入库时间:">
					<el-date-picker
						v-model="dataForm.addTime"
						value-format="YYYY-MM-DD HH:mm:ss"
						type="datetime"
					/>
				</el-form-item>
				<el-form-item class="divlineright" label="存放位置:">
					<el-select v-model="dataForm.siteId">
						<el-option
						v-for="item in dataForm.houselist"
						:key="item.value"
						:label="item.label"
						:value="item.value"
						/>
					</el-select>
				</el-form-item>
			</div>
			<div class="divline">
				<el-form-item class="divlineleft" prop="supplierName" label="药品供应商:">
					<el-input v-model="dataForm.supplierName" @click="supplierHandle()" placeholder="" clearable></el-input>
				</el-form-item>
				<el-form-item class="divlinecenter" prop="docNo" label="随货单号:">
					<el-input v-model="dataForm.docNo" placeholder="" clearable disabled="true"></el-input>
				</el-form-item>
				<el-form-item class="divlineright" prop="invoiceNo" label="发票号:">
					<el-input v-model="dataForm.invoiceNo" placeholder="" clearable></el-input>
				</el-form-item>
			</div>
			<div class="divline">
				<el-form-item class="divlineleft" prop="createName" label="制单人:">
					<el-input v-model="dataForm.createName" placeholder="" clearable></el-input>
				</el-form-item>
				<el-form-item class="divlinecenter" prop="remark" style="width: 61.5%;" label="备注:">
					<el-input v-model="dataForm.remark" placeholder="" clearable></el-input>
				</el-form-item>
			</div>
			<div class="menuline">
				<div class="menutit">单据药品清单</div>
				<el-form-item class="menuleft">
					<el-button v-auth="'sys:post:save'" type="primary" @click="addOrUpdateHandle()">添加药品</el-button>
					<el-button  type="primary" @click="submitHandle()">保存</el-button>
				</el-form-item>
			</div>
		</el-form>
		<el-table :data="state.dataList" border style="width: 100%">
			<el-table-column label="序号" type="index" width="60"></el-table-column>
			<el-table-column prop="goodName" label="药品名称" header-align="center" align="center"></el-table-column>
			<el-table-column prop="goodSpec" label="药品规格" header-align="center" align="center"></el-table-column>
			<el-table-column prop="goodCount" label="商品数量" header-align="center" align="center">
				<template #default="scope">
					<el-input @change="changePrice(scope.row)"
						v-model="scope.row.goodCount"
					></el-input>
                </template>
			</el-table-column>
			<el-table-column prop="goodSource" label="药品厂家" header-align="center" align="center"></el-table-column>
			<el-table-column prop="goodPrice" label="药品进价" header-align="center" align="center"></el-table-column>
			<el-table-column prop="salePrice" label="销售价格" header-align="center" align="center"></el-table-column>
			<el-table-column prop="goodUnit" label="药品单位" header-align="center" align="center"></el-table-column>
			<el-table-column prop="goodTime" label="生产日期" header-align="center" align="center"></el-table-column>
			<el-table-column prop="timeArea" label="有效期" header-align="center" align="center"></el-table-column>
			<el-table-column prop="totalPrice" label="合计价格" header-align="center" align="center"></el-table-column>
		</el-table>
		<!-- 弹窗, 新增 / 修改 -->
		<add-or-update ref="addOrUpdateRef" @fatherMethod="fatherMethod" @refresh-data-list="getDataList"></add-or-update>
		<!-- 弹窗, 新增 / 修改 -->
		<checkSupplier ref="supplierRef" @fatherSupMethod="fatherSupMethod" @refresh-data-list="getDataList"></checkSupplier>
	</el-card>
</template>

<script setup lang="ts" name="SysPostIndex">
import { useCrud } from '@/hooks'
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import AddOrUpdate from './datalist.vue'
import checkSupplier from './supplierlist.vue'
import { IHooksOptions } from '@/hooks/interface'
import { useI18n } from 'vue-i18n'
import { useHouseApi ,useHouseDetailApi,useHouseListApi,useHouseDetailListApi} from '@/api/sys/good'
const { t } = useI18n()
const state: IHooksOptions = reactive({
	deleteUrl: '',
	dataList:[
		{
			id:"",
			uid:"",
			parentId:"",
			goodId:"",
			goodName:"",
			goodType:"",
			goodSource:"",
			goodSpec:"",
			goodTime:"",
			timeArea:"",
			goodCount:"",
			goodUnit:"",
			orgId:"",
			goodPrice:"",
			salePrice:"",
			totalPrice:""
		}
	],
})

const dataFormRef = ref()
const loginRules = ref({
	invoiceNo: [{ required: true, message: t('required'), trigger: 'blur' }],
	siteId: [{ required: true, message: t('required'), trigger: 'blur' }],
	// docNo: [{ required: true, message: t('required'), trigger: 'blur' }],
	addTime: [{ required: true, message: t('required'), trigger: 'blur' }],
	createName: [{ required: true, message: t('required'), trigger: 'blur' }],
	supplierId: [{ required: true, message: t('required'), trigger: 'blur' }],
	addType: [{ required: true, message: t('required'), trigger: 'blur' }]
})
const dataForm = reactive({
		addType:"",
		supplierId:"",
		supplierName:"",
		createName:localStorage.getItem('realName'),
		addTime:"",
		docNo:"",
		remark:"",
		siteId:"",
		invoiceNo:"",
		orgId:localStorage.getItem('org'),
		typelist:[
			{
				value: '1',
				label: '采购入库',
			},
			{
				value: '2',
				label: '科室回收',
			},
			{
				value: '3',
				label: '调拨入库',
			}
		],
		houselist:[
			{
				value: '1',
				label: '西药房',
			},
			{
				value: '2',
				label: '中药房',
			}
		],
})
const addOrUpdateRef = ref()
const addOrUpdateHandle = () => {
	getDocNo();
	addOrUpdateRef.value.init()
}
const supplierRef = ref()
const supplierHandle = () => {
	supplierRef.value.init()
}
const fatherSupMethod=(name:any,uid:any)=>{
	dataForm.supplierId=uid;
	dataForm.supplierName=name;
}
const getDocNo=()=>{
	let dt = new Date()
            var y = dt.getFullYear()
            var mt = (dt.getMonth() + 1).toString().padStart(2,'0')
            var day = dt.getDate().toString().padStart(2,'0')
            var h = dt.getHours().toString().padStart(2,'0')
            var m = dt.getMinutes().toString().padStart(2,'0')
			var s = dt.getSeconds().toString().padStart(2,'0')
    dataForm.docNo = "PO"+y + "" + mt + "" + day + "" + h + "" + m +s;
}
const fatherMethod=(obj:any)=>{
	let dataArr=[];
	console.log(obj);
	for(let index=0;index<obj.length;index++){
		
		dataArr[index]={
			"uid":obj[index].uid,
			"goodName":obj[index].goodName,
			"goodPrice":obj[index].goodPrice,
			"goodSource":obj[index].goodSource,
			"goodSpec":obj[index].goodSpec,
			"goodTime":obj[index].goodTime,
			"goodType":obj[index].goodType,
			"goodUnit":obj[index].goodUnit,
			"orgId":obj[index].orgId,
			"goodCount":1,
			"salePrice":obj[index].salePrice,
			"timeArea":obj[index].timeArea,
			"goodId":obj[index].uid,
			"totalPrice":obj[index].salePrice
		};
		console.log(obj[index]);
	}
	state.dataList=dataArr;
}
const changePrice=(row:any)=>{
	row.totalPrice=(row.goodCount*row.salePrice).toFixed(2);
}
// 表单提交
const submitHandle = () => {
	let dataArray:any=state.dataList;
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}
		useHouseApi(dataForm).then((msg:any) => {
			let parentId=msg.id;
			for(let index=0;index<dataArray.length;index++){
				dataArray[index]={
					"uid":dataArray[index].uid,
					"goodName":dataArray[index].goodName,
					"goodPrice":dataArray[index].goodPrice,
					"goodSource":dataArray[index].goodSource,
					"goodSpec":dataArray[index].goodSpec,
					"goodTime":dataArray[index].goodTime,
					"goodType":dataArray[index].goodType,
					"goodUnit":dataArray[index].goodUnit,
					"orgId":dataArray[index].orgId,
					"goodCount":dataArray[index].goodCount,
					"salePrice":dataArray[index].salePrice,
					"timeArea":dataArray[index].timeArea,
					"goodId":dataArray[index].goodId,
					"parentId":parentId,
					"totalPrice":dataArray[index].totalPrice,
				};
			}
			useHouseDetailApi(dataArray).then(() => {
			})
		})
		useHouseListApi(dataForm).then((msg:any) => {
			let parentId=msg.id;
			for(let index=0;index<dataArray.length;index++){
				dataArray[index]={
					"uid":dataArray[index].uid,
					"goodName":dataArray[index].goodName,
					"goodPrice":dataArray[index].goodPrice,
					"goodSource":dataArray[index].goodSource,
					"goodSpec":dataArray[index].goodSpec,
					"goodTime":dataArray[index].goodTime,
					"goodType":dataArray[index].goodType,
					"goodUnit":dataArray[index].goodUnit,
					"orgId":dataArray[index].orgId,
					"goodCount":dataArray[index].goodCount,
					"salePrice":dataArray[index].salePrice,
					"timeArea":dataArray[index].timeArea,
					"goodId":dataArray[index].goodId,
					"parentId":parentId,
					"totalPrice":dataArray[index].totalPrice,
				};
			}
			useHouseDetailListApi(dataArray).then(() => {
				ElMessage.success({
					message: '操作成功',
					duration: 500
				})
				state.dataList=[];
				dataFormRef.value.resetFields()
			})
		})
		
	})
}
const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle } = useCrud(state)
</script>

<style lang="scss" scoped>
.divline{
	width: 100%;
	height: 50px;
	float: left;
}
.divlinecenter{
	width: 30%;
	height: 30px;
	float: left;
}
.divlineleft{
	width: 30%;
	height: 30px;
	float: left;
}
.divlineright{
	width: 30%;
	height: 30px;
	float: left;
}
.menuline{
	width: 100%;
	height: 50px;
	float: left;
}
.menutit{
	width: 80%;
	height: 50px;
	float: left;
	text-align: center;
	font-size: 20px;
	font-weight: bold;
	line-height: 3;
}
.menuleft{
	width: 17%;
	float: left;
	height: 50px;
}
</style>
